package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.common.response.JsonResult;
import cn.tedu._05mvcboot03.common.response.StatusCode;
import cn.tedu._05mvcboot03.mapper.ArticleMapper;
import cn.tedu._05mvcboot03.mapper.UserMapper;
import cn.tedu._05mvcboot03.pojo.dto.UserArticleDTO;
import cn.tedu._05mvcboot03.pojo.dto.UserLoginDTO;
import cn.tedu._05mvcboot03.pojo.entity.Article;
import cn.tedu._05mvcboot03.pojo.vo.ArticleVO;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Api(tags = "开发模块")
@Slf4j
@RequestMapping("/v1/users/")
@RestController
@Validated
public class Controller {
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录功能
     * 请求地址：/v1/users/login
     * 请求方法：POST
     * 请求类型: application/json
     * 请求体数据：UserLoginDTO
     */
    @ApiOperation("登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        log.debug("userLoginDTO = " + userLoginDTO);
        UserVO userVO = userMapper.selectByName(userLoginDTO.getUsername(), userLoginDTO.getPassword());
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);//用户名或密码错误
        }
        session.setAttribute("user", userVO);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);//操作成功
    }

    @Autowired
    private ArticleMapper articleMapper;

    /**
     *  发表文章功能
     * 请求地址：/v1/users/article
     * 请求方法：POST
     * 请求体类型: application/json
     * 请求体数据：UserArticleDTO
     */
    @ApiOperation("发表文章功能")
    @PostMapping("article")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO,@ApiIgnore HttpSession session){
        log.debug("userArticleDTO = " + userArticleDTO);
        UserVO  userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);//未登录
        }
        Article article = new Article();
        BeanUtils.copyProperties(userArticleDTO, article);
        article.setUserId(userVO.getId());
        articleMapper.insertArticle(article);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);//成功
    }

    /**
     * 查询指定文章功能
     * 请求地址：/v1/users/selectByArticleId
     * 请求方法：GET
     * 查询参数：文章id 【要求在1-100之间】
     */
    @ApiOperation("查询指定文章功能")
    @GetMapping("selectByArticleId")
    public JsonResult selectByArticleId(@RequestParam @Range(min = 1, max = 100, message = "文章编码限定在1~100之间") Integer id){
        log.debug("id = " + id);
        ArticleVO articleVO = articleMapper.selectById(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS, articleVO);
    }
}
